﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OSGeo.MapGuide;

namespace IGIS.Core
{
    public class FeatureLog
    {
        // Fields
        private readonly Feature _feature;
        private readonly HtzMgHelper _helper;

        // Methods
        public FeatureLog(HtzMgHelper helper, Feature feature)
        {
            this._helper = helper;
            this._feature = feature;
            MgFeatureQueryOptions opt = new MgFeatureQueryOptions();
            opt.SetFilter(string.Format("ItemId={0}", feature.FeatId));
            MgFeatureReader reader = this._helper.FeatureService.SelectFeatures(new MgResourceIdentifier(this._feature.Layer.FeatureSourceId), feature.Layer.FeatureClassName + "Log", opt);
            List<Dictionary<string, string>> logItems = new List<Dictionary<string, string>>();
            while (reader.ReadNext())
            {
                Dictionary<string, string> logItem;
                logItem = new Dictionary<string, string> {
                
            };
                foreach (KeyValuePair<string, FeatureProperty> prop in feature.Properties)
                {
                    string val = this._helper.GetFeaturePropertyValue(reader, prop.Key, true).ToString();
                    logItem.Add(prop.Value.Alias, val);
                }
            }
            logItems.Reverse();
            this.Entries = logItems.ToArray();
        }

        // Properties
        public Dictionary<string, string>[] Entries { get; set; }

        public string FeatureClassName { get; set; }

        public string FeatureLogClassName
        {
            get
            {
                return (this.FeatureClassName + "Log");
            }
        }

        public int ItemId { get; set; }

        public MgLayer ItemLayer { get; set; }
    }


}